package com.int_tech.api.controller;

import com.int_tech.api.etity.*;
import com.int_tech.api.service.ApiService;
import com.int_tech.car.entity.Car;
import com.int_tech.control.entity.SocketLogSend;
import com.int_tech.mession.entity.Mession;
import com.int_tech.station.entity.Station;
import com.int_tech.user.dao.UserDao;
import com.int_tech.user.entity.User;
import com.int_tech.user.servise.UserService;
import com.int_tech.utils.KuaYu;
import com.int_tech.utils.MulServerSocket;
import com.int_tech.utils.Result;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.UnknownHostException;
import java.util.List;

@Controller
@RequestMapping("api")
public class ApiController {

    @Autowired
    private ApiService apiService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserDao userDao;

    /**
     * 校验用户名密码
     *
     * @param request
     * @param username
     * @return
     * @throws UnsupportedEncodingException
     * @throws UnknownHostException
     */
    @RequestMapping(value = "/checkLogin", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String checkPower(HttpServletRequest request, String username, String password, HttpServletResponse response) {
        try{
            KuaYu.kuaYuResponse(response);//跨域支持
        }catch (Exception ex){
            return  Result.build(-1,"登录异常,请联系管理员!").toString();
        }
        User user = userDao.findUserByNameAndPassword(username, password);
        JSONObject object = new JSONObject();
        if (user == null) {
            object.put("status", "fail");
            object.put("msg", "用户名或密码不正确");
        } else {
            //共享session接口获取存放
            object.put("status", "success");

            JSONObject jsonObject = new JSONObject();

            if (user.getType() == 1) {
                request.getSession().setAttribute("username", "admin");

                jsonObject.put("key1","username");
                jsonObject.put("value1","admin");

            } else if (user.getType() == 2) {
                request.getSession().setAttribute("username", "client");

                jsonObject.put("key1","username");
                jsonObject.put("value1","client");
            } else if (user.getType() == 3) {
                request.getSession().setAttribute("username", "user");

                jsonObject.put("key1","username");
                jsonObject.put("value1","user");
            }


            request.getSession().setAttribute("userInfoSession", username + "," + password);
//            request.getSession().setAttribute("clientId", user.getClientId());
            request.getSession().setAttribute("clientId", String.valueOf(user.getId()));
            request.getSession().setAttribute("uid", user.getId());

            jsonObject.put("key2","userInfoSession");
            jsonObject.put("value2",username + "," + password);

            jsonObject.put("key3","clientId");
            jsonObject.put("value3",String.valueOf(user.getId()));

            jsonObject.put("key4","uid");
            jsonObject.put("value4",user.getId());


            MulServerSocket.httpClientPost("http://39.104.171.185:8080/car_networking/user/saveOtherSession",jsonObject.toString());
//            MulServerSocket.httpClientPost("http://39.104.181.194:8080/car_networking/user/saveOtherSession",jsonObject.toString());

        }


        return object.toString();
    }

    /**
     * 动态查询汽车接口
     * @param carSearchUtil
     * @param pageUtil
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCarAndDynamic", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String getCarAndDynamic(CarSearchUtil carSearchUtil, PageUtil pageUtil, HttpServletRequest request, HttpServletResponse response) {
        try {
            KuaYu.kuaYuResponse(response);//跨域支持
            Page<Car> cars = apiService.getCarAndDynamic(carSearchUtil,pageUtil);
            if(cars!=null){
                JSONObject data = JSONObject.fromObject(cars);
                data.put("status",200);
                data.put("msg","获取成功");
                return data.toString();
            }else{
                return  Result.build(0,"未查询到相关汽车!").toString();
            }
        }catch (Exception ex){
            return  Result.build(-1,"查询汽车接口异常!").toString();
        }
    }

    /**
     * 动态查询电子锁任务接口
     * @param messionSearchUtil
     * @param pageUtil
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getMessionAndDynamic", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String getMessionAndDynamic(MessionSearchUtil messionSearchUtil, PageUtil pageUtil, HttpServletRequest request, HttpServletResponse response) {
        try {
            KuaYu.kuaYuResponse(response);//跨域支持
            Page<Mession> cars = apiService.getMessionAndDynamic(messionSearchUtil,pageUtil);
            if(cars!=null){
                JSONObject data = JSONObject.fromObject(cars);
                data.put("status",200);
                data.put("msg","获取成功");
                return data.toString();
            }else{
                return  Result.build(0,"未查询到相关电子锁任务!").toString();
            }
        }catch (Exception ex){
            return  Result.build(-1,"查询电子锁任务接口异常!").toString();
        }
    }

    /**
     * 动态查询场站接口
     * @param stationSearchUtil
     * @param pageUtil
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getStationAndDynamic", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String getStationAndDynamic(StationSearchUtil stationSearchUtil, PageUtil pageUtil, HttpServletRequest request, HttpServletResponse response) {
        try {
            KuaYu.kuaYuResponse(response);//跨域支持
            Page<Station> cars = apiService.getStationAndDynamic(stationSearchUtil,pageUtil);
            if(cars!=null){
                JSONObject data = JSONObject.fromObject(cars);
                data.put("status",200);
                data.put("msg","获取成功");
                return data.toString();
            }else{
                return  Result.build(0,"未查询到相关场站!").toString();
            }
        }catch (Exception ex){
            return  Result.build(-1,"查询场站接口异常!").toString();
        }
    }

    /**
     * 动态查询请求日志接口
     * @param socketLogSendSearchUtil
     * @param pageUtil
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getSocketLogSendAndDynamic", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String getSocketLogSendAndDynamic(SocketLogSendSearchUtil socketLogSendSearchUtil, PageUtil pageUtil, HttpServletRequest request, HttpServletResponse response) {
        try {
            KuaYu.kuaYuResponse(response);//跨域支持
            Page<SocketLogSend> cars = apiService.getSocketLogSendAndDynamic(socketLogSendSearchUtil,pageUtil);
            if(cars!=null){
                JSONObject data = JSONObject.fromObject(cars);
                data.put("status",200);
                data.put("msg","获取成功");
                return data.toString();
            }else{
                return  Result.build(0,"未查询到请求日志!").toString();
            }
        }catch (Exception ex){
            return  Result.build(-1,"查询请求接口异常!").toString();
        }
    }
}
